
#include "HttpCmdTestSqlite.h"
#include "SysInfo.h"
#include "Log_Client.h"
#include <map>

CHttpCmdTestSqlite::CHttpCmdTestSqlite()
{
    ClearCmdLocalData();
}


CHttpCmdTestSqlite::~CHttpCmdTestSqlite()
{

}

void CHttpCmdTestSqlite::ClearCmdLocalData()
{
    m_nCmdType = TYPE_INVALID;
    m_nCount = 0;
    m_strText = "";
}

bool CHttpCmdTestSqlite::CheckParamVaild(const std::string &strReqMessage)
{
    neb::CJsonObject reqJsonObj(strReqMessage);
    neb::CJsonObject &dataJsonObj = reqJsonObj["Data"];


    dataJsonObj.Get("Count", m_nCount);
    dataJsonObj.Get("Text", m_strText);


    std::string strTmp;
    dataJsonObj.Get("CmdType", strTmp);
    m_nCmdType = GetTypeEnumByString(strTmp);

    if (TYPE_INVALID == m_nCmdType)
    {
        return false;
    }


    printf("strCmdType = %s(%d), m_nCount = %d, m_strText = %s\n", strTmp.c_str(), m_nCmdType, m_nCount, m_strText.c_str());

    return true;
}

LOG_CMD_TYPE CHttpCmdTestSqlite::GetTypeEnumByString(const std::string &strCmdType)
{

    std::map<std::string, LOG_CMD_TYPE> LogTypeMap;
    LogTypeMap["addLog"] = TYPE_ADD_LOG;
    LogTypeMap["delLog"] = TYPE_DEL_LOG;
    LogTypeMap["clrLog"] = TYPE_CLEAR_LOG;


    std::map<std::string, LOG_CMD_TYPE>::iterator iter = LogTypeMap.find(strCmdType);
    if (iter == LogTypeMap.end())
    {
        printf("[Error] error type %s in TestSqlite\n", strCmdType.c_str());
        return TYPE_INVALID;
    }

    LOG_CMD_TYPE nCmdType = iter->second;

    return nCmdType;
}

bool CHttpCmdTestSqlite::FillRspData(std::string &strRspData)
{
    std::string strResult;
    bool ret = ExecuteCommand();
    if (!ret)
    {
        strResult = "Execute Fail";
    }
    else
    {
        strResult = "Execute Successfully";
    }

    // need some system helper functions
    neb::CJsonObject rspJsonObj;
    rspJsonObj.AddEmptySubObject("Data");
    neb::CJsonObject &dataJsonObj = rspJsonObj["Data"];

    dataJsonObj.Add("Status", strResult);    

    strRspData = rspJsonObj.ToString();
    return ret;
}

bool CHttpCmdTestSqlite::ExecuteCommand()
{
    switch (m_nCmdType)
    {
        case TYPE_ADD_LOG:
            AddLog(INFO_LEVEL, "Http", m_strText.c_str());
            break;
        case TYPE_DEL_LOG:
            DeleteLog(m_nCount);
            break;
        case TYPE_CLEAR_LOG:
            ClearLog();
            break;

    default:
        break;
    }


    return true;
}